package com.kx.service_exam.service.impl;

import com.kx.service_exam.dao.QuestionChoiceMapper;
import com.kx.service_exam.dao.QuestionChoiceMapper_My;
import com.kx.service_exam.exception.BusinessException;
import com.kx.service_exam.model.QuestionChoice;
import com.kx.service_exam.model.QuestionChoiceExample;
import com.kx.service_exam.service.interfaces.IQuestionChoiceSV;
import com.kx.service_exam.vo.query.QuestionChoiceQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class QuestionChoiceSVImpl implements IQuestionChoiceSV{

    @Autowired
    private QuestionChoiceMapper questionChoiceMapper;

    @Autowired
    private QuestionChoiceMapper_My questionChoiceMapper_my;

    @Override
    public int insertQuestionChoice(QuestionChoice questionChoice) {
        Date date = new Date();
        questionChoice.setId(UUID.randomUUID().toString());
        questionChoice.setCreate_time(date);
        questionChoice.setModify_time(date);
        return questionChoiceMapper.insert(questionChoice);
    }

    @Override
    public int batchInsertQuestionChoice(List<QuestionChoice> questionChoices, String questionId, String operatorId) {
        if(StringUtils.isEmpty(questionId)){
            throw new BusinessException("参数[questionId]不能为空!");
        }
        if(questionChoices.isEmpty()){
            throw new BusinessException("参数[questionChoices]不能为空!");
        }
        Date date = new Date();
        for (QuestionChoice questionChoice : questionChoices) {
            questionChoice.setQuestion_id(questionId);
            questionChoice.setId(UUID.randomUUID().toString());
            questionChoice.setDelete_flag(0);
            questionChoice.setCreate_time(date);
            questionChoice.setModify_time(date);
            questionChoice.setCreator(operatorId);
            questionChoice.setModifier(operatorId);
        }
        return questionChoiceMapper_my.batchInsertQuestionChoice(questionChoices);
    }

    @Override
    public int batchDeleteQuestionChoice(String[] ids) {
        if(ids == null || ids.length == 0){
            throw new BusinessException("参数[ids]不能为空!");
        }
        return questionChoiceMapper_my.batchDeleteQuestionChoice(ids);
    }

    @Override
    public int logicalDeleteQuestionChoice(QuestionChoice questionChoice) {
        questionChoice.setDelete_flag(1);
        questionChoice.setModify_time(new Date());
        return questionChoiceMapper.updateByPrimaryKeySelective(questionChoice);
    }

    @Override
    public int logicalDeleteQuestionChoiceByQuestionId(String questionId) {
        return questionChoiceMapper_my.logicalDeleteQuestionChoiceByQuestionId(questionId);
    }

    @Override
    public int deleteQuestionChoice(String id) {
        return questionChoiceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteQuestionChoiceByQuestionId(String questionId) {
        if(StringUtils.isEmpty(questionId)){
            throw new BusinessException("参数[questionId]不能为空!");
        }
        return questionChoiceMapper_my.deleteQuestionChoiceByQuestionId(questionId);
    }

    @Override
    public int updateQuestionChoice(QuestionChoice questionChoice) {
        questionChoice.setModify_time(new Date());
        return questionChoiceMapper.updateByPrimaryKeySelective(questionChoice);
    }

    @Override
    public QuestionChoice selectQuestionChoiceById(String id) {
        return questionChoiceMapper.selectByPrimaryKey(id);
    }

    @Override
    public long countQuestionChoiceByFilter(QuestionChoiceQueryVO filter) throws ParseException {
        return questionChoiceMapper.countByExample(this.convertFilterToExample(filter));
    }

    @Override
    public long countQuestionChoiceByQuestionId(String questionId) throws ParseException {
        if(StringUtils.isEmpty(questionId)) {
            throw new BusinessException("参数[questionId]不能为空!");
        }
        QuestionChoiceQueryVO filter = new QuestionChoiceQueryVO();
        filter.setQuestion_id(questionId);
        filter.setDelete_flag(0);
        return questionChoiceMapper.countByExample(this.convertFilterToExample(filter));
    }

    @Override
    public List<QuestionChoice> listQuestionChoiceByFilter(QuestionChoiceQueryVO filter) throws ParseException {
        return questionChoiceMapper.selectByExample(this.convertFilterToExample(filter));
    }

    @Override
    public List<QuestionChoice> listQuestionChoiceByQuestionId(String questionId) throws ParseException {
        if(StringUtils.isEmpty(questionId)) {
            throw new BusinessException("参数[questionId]不能为空!");
        }
        QuestionChoiceQueryVO filter = new QuestionChoiceQueryVO();
        filter.setQuestion_id(questionId);
        filter.setDelete_flag(0);
        return questionChoiceMapper.selectByExample(this.convertFilterToExample(filter));
    }

    @Override
    public List<QuestionChoice> listAnswerByQuestionId(String questionId) throws ParseException {
        if(StringUtils.isEmpty(questionId)) {
            throw new BusinessException("参数[questionId]不能为空!");
        }
        QuestionChoiceQueryVO filter = new QuestionChoiceQueryVO();
        filter.setQuestion_id(questionId);
        filter.setDelete_flag(0);
        filter.setTure_flag(1);
        return questionChoiceMapper.selectByExample(this.convertFilterToExample(filter));
    }

    private QuestionChoiceExample convertFilterToExample(QuestionChoiceQueryVO filter) throws ParseException {
        if(filter == null ) {
            return null;
        }
        QuestionChoiceExample questionChoiceExample = new QuestionChoiceExample();
        QuestionChoiceExample.Criteria criteria = questionChoiceExample.or();
        if(!StringUtils.isEmpty(filter.getQuestion_id())) {
            criteria.andQuestion_idEqualTo(filter.getQuestion_id());
        }
        if (!StringUtils.isEmpty(filter.getTure_flag())) {
            criteria.andTure_flagEqualTo(filter.getTure_flag());
        }
        if (!StringUtils.isEmpty(filter.getDelete_flag())) {
            criteria.andDelete_flagEqualTo(filter.getDelete_flag());
        }
        return questionChoiceExample;
    }
}
